package com.project.quiktask.controller;

import java.sql.Date;
import java.sql.Time;
import java.util.Comparator;

import com.project.quiktask.Task;
import com.project.quiktask.util.Variable;

public class StartTimeComparator implements Comparator<Task> {

	@Override
	public int compare(Task taskOne, Task taskTwo) {
	       
    	Date taskOneStartDate = taskOne.getStartDate();
    	Time taskOneStartTime = taskOne.getStartTime();
    			
    	Date taskTwoStartDate = taskTwo.getStartDate();
    	Time taskTwoStartTime = taskTwo.getStartTime();
    	
    	int result = getMoreUrgentTask(taskOneStartDate, taskTwoStartDate, taskOneStartTime, taskTwoStartTime);
    	
    	return result;
    }
    
    public int getMoreUrgentTask (Date date1, Date date2, Time time1, Time time2) {
    	
    	int result;
    	
    	if (isDateNull(date1) && !isDateNull(date2))
    		return Variable.SECOND_TASK_MORE_URGENT;
    	else if (!isDateNull(date1) && isDateNull(date2))
    		return Variable.FIRST_TASK_MORE_URGENT;
    	else {
    		result = date1.compareTo(date2);
        	if (result != Variable.EQUAL_URGENCY)
        		return result;
        	else {
        		if (isTimeNull(time1) && !isTimeNull(time2)) 
        			return Variable.SECOND_TASK_MORE_URGENT;
        		else if (!isTimeNull(time1) && isTimeNull(time2))
            		return Variable.FIRST_TASK_MORE_URGENT;
            	else {
            		result = time1.compareTo(time2);
            		if (result < Variable.EQUAL_URGENCY)
            			return Variable.FIRST_TASK_MORE_URGENT;
            		else
            			return Variable.SECOND_TASK_MORE_URGENT;
        		}
        	}
    	}
    }
    
    public boolean isDateNull(Date date) {
    	if (date == null)
    		return true;
    	else 
    		return false;
    }
    
    public boolean isTimeNull(Time time) {
    	if (time==null)
    		return true;
    	else
    		return false;
    }

}
